package com.nine.shop.controller;


import com.nine.shop.common.ServerResponse;

import com.nine.shop.model.User;
import com.nine.shop.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author chenyi
 * Create on 2019/8/17
 */
@RestController

@RequestMapping(value = "api/shopping/user", method = RequestMethod.POST)
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    /**
     * 用户登录
     *
     * @param
     * @param
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "login.do")
    public ServerResponse login(User user) {

        //  user.setUserName("1234");
        //user.setPassword("1234");
        ServerResponse message = userService.login(user);
        if (message.getData() != null) {
            super.setUser((User) message.getData());
        }
        return message;
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @CrossOrigin
    @RequestMapping("register.do")
    public ServerResponse register(User user) {

        // user.setUserName("1234");
        // user.setPassword("1234");
        // user.setEmail("1234");
        ServerResponse message = userService.register(user);
        if (message.getData() != null) {
            super.setUser(user);
        }
        return message;

    }

    /**
     * 修改用户密码
     *
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "updatepassword.do")
    public ServerResponse updatePassword(String oldPassword, String newPassword) {
        int id = super.getUser().getId();
        return (ServerResponse) userService.updatePassword(id, oldPassword, newPassword);
    }

}
